package com.sspaas.message.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.sspaas.message.pojo.Message;
import com.sspaas.message.pojo.MessageResult;
import com.sspaas.message.pojo.SuperMessage;
import com.sspaas.message.utils.ShortMsgUtils;

@Service
public class MessageService {
	/**
	 * 单一短信发送校验
	 * 
	 * @param message
	 * @return
	 */
	public MessageResult sendShortMsg(Message message) {
		// 判断 message参数
		MessageResult result = new MessageResult();
		if (!message.getPhone().matches("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$")) {
			result.setStatusCode(102);
			result.setStatusMsg("手机号格式错误");
			return result;
		}
		if (!message.getTemplet().matches("^[1-9]\\d*$")) {
			result.setStatusCode(103);
			result.setStatusMsg("短信模板错误");
			return result;
		}
		if (message.getPhoneCode() == null || message.getPhoneCode().length == 0) {
			result.setStatusCode(104);
			result.setStatusMsg("未传递参数");
			return result;
		}
		for (String code : message.getPhoneCode()) {
			if (code.isEmpty()) {
				result.setStatusCode(105);
				result.setStatusMsg("参数为空");
				return result;
			}
		}
		// 执行短信结果
		HashMap<String, Object> sendShortMsg = ShortMsgUtils.sendShortMsg(message.getPhone(), message.getTemplet(),
				message.getPhoneCode());
		if ("000000".equals(sendShortMsg.get("statusCode"))) {
			// 正常返回输出data包体信息（map）
			result.setStatusCode(MessageResult.OK);
			result.setStatusMsg((String) sendShortMsg.get("statusMsg"));
		} else {
			result.setStatusCode(101);
			result.setStatusMsg((String) sendShortMsg.get("statusMsg"));
		}
		return result;
	}

	/**
	 * 短信群发功能
	 * 
	 * @param superMsg
	 * @return
	 */
	public MessageResult sendShortMsgList(SuperMessage superMsg) {
		MessageResult result = new MessageResult();
		List<String> phoneList = superMsg.getPhoneList();
		String s = "";
		for (String phone : phoneList) {
			s += phone + ",";
			if (!phone.matches("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$")) {
				result.setStatusCode(102);
				result.setStatusMsg("手机号格式错误");
				return result;
			}
		}
		if (!superMsg.getTemplet().matches("^[1-9]\\d*$")) {
			result.setStatusCode(103);
			result.setStatusMsg("短信模板错误");
			return result;
		}
		if (superMsg.getPhoneCode() == null || superMsg.getPhoneCode().length == 0) {
			result.setStatusCode(104);
			result.setStatusMsg("未传递任何参数");
			return result;
		}
		for (String code : superMsg.getPhoneCode()) {
			if (code.isEmpty()) {
				result.setStatusCode(105);
				result.setStatusMsg("多个参数中,有空值");
				return result;
			}
		}
		// 获取所有的手机集合","分割
		String substring = s.substring(0, s.length() - 1);
		HashMap<String, Object> sendShortMsg = ShortMsgUtils.sendShortMsg(substring, superMsg.getTemplet(),
				superMsg.getPhoneCode());
		if ("000000".equals(sendShortMsg.get("statusCode"))) {
			// 正常返回输出data包体信息（map）
			result.setStatusCode(MessageResult.OK);
			result.setStatusMsg((String) sendShortMsg.get("statusMsg"));
		} else {
			result.setStatusCode(101);
			result.setStatusMsg((String) sendShortMsg.get("statusMsg"));
		}
		return result;
	}

}
